<template>
  <div class="supply-component">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="发布内容" prop="message">
        <el-input v-model="ruleForm.message"></el-input>
      </el-form-item>
      <el-form-item label="信息类型" prop="type">
        <el-select v-model="ruleForm.type" placeholder="请选择信息类型">
          <el-option label="求购" name="type" value="0"></el-option>
          <el-option label="收购" name="type" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="ruleForm.phone"></el-input>
      </el-form-item>
      <el-form-item label="联系地址" prop="address">
        <el-input v-model="ruleForm.address"></el-input>
      </el-form-item>
      <el-form-item label="详细描述" prop="descInfo">
        <el-input type="textarea" v-model="ruleForm.descInfo"></el-input>
      </el-form-item>
      <el-form-item label="图片描述" prop="imageDesc">
        <el-input v-model="ruleForm.imageDesc" style="display: none"></el-input>
        <image-upload></image-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">发布信息</el-button>
        <el-button @click="resetForm('ruleForm')">重置信息</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {sendMessage} from '../../api/message'
import { getUserId } from '@/libs/util'
import ImageUpload from '@/components/upload/ImageUpload'
import bus from '../../assets/js/eventBus'
import {getCurrentStringTime} from '../../utils/dateUtils'
export default {
  name: 'SupplyComponent',
  components: {ImageUpload},
  mounted () {
    // 每次绑定前先解绑，防止事件多次触发
    bus.$off('setImageName')
    bus.$on('setImageName', target => {
      this.imageList.push(target)
      console.log('this.imageList.toString：', this.imageList.toString())
      this.ruleForm.imageDesc = this.imageList.toString()
    })
  },
  data () {
    return {
      imageList: [],
      ruleForm: {
        message: '',
        type: '',
        phone: '',
        address: '',
        desc: '',
        userId: getUserId(),
        imageDesc: '',
        createTime: getCurrentStringTime()
      },
      rules: {
        message: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 5, max: 20, message: '长度在 5 到 20 个字符', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请选择发布信息类型', trigger: 'change' }
        ],
        phone: [
          { required: true, message: '请填写联系电话', trigger: 'change' }
        ],
        address: [
          { required: true, message: '请填写联系地址', trigger: 'change' }
        ],
        descInfo: [
          { required: true, message: '请填写信息详情', trigger: 'blur' }
        ],
        imageDesc: [
          { required: true, message: '请添加图片详情', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm (formName) {
      console.log(this.ruleForm.createTime)
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const promise = sendMessage(this.ruleForm)
          promise.then(result => {
            if (result.status === 200) {
              this.$alert(result.message, '成功', {
                confirmButtonText: '确定',
                callback: action => {
                  this.resetForm(formName)
                }
              })
            }
          }).catch(error => {
            console.log(error)
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style scoped>
  .supply-component{
    padding: 50px 20%;
    width:60%;
    min-height:800px;
    height:100%;
    background-color:#FFF;
  }

</style>
